<template>
  <div class="chat-container">
    <div class="chat-header">
      <h2>实时聊天</h2>
    </div>

    <div class="chat-messages" ref="messageList">
      <!-- 聊天消息显示区域 -->
      <div v-for="msg in messages" :key="msg.id" class="chat-message">
        <p><strong>{{ msg.senderName }}:</strong> {{ msg.content }}</p>
      </div>
    </div>

    <div class="chat-input">
      <input type="text" v-model="message" placeholder="输入消息" @keyup.enter="sendMessage" />
      <button @click="sendMessage">发送</button>
    </div>
  </div>
</template>

<script>
import { ref, onMounted, onBeforeUnmount } from 'vue';
import { useRoute } from 'vue-router';
import { ElMessage } from 'element-plus';
import { connectWebSocket, sendMessage, fetchChatHistory, submitDispute } from '@/api/chat';

export default {
  setup() {
    const message = ref(''); // 存储输入的消息
    const messages = ref([]); // 存储聊天记录
    const ws = ref(null); // WebSocket 实例
    const route = useRoute(); // 获取路由信息，包含订单、任务等信息
    const heartbeatInterval = 30000; // 心跳间隔时间，单位毫秒
    const reconnectInterval = 5000; // 重新连接间隔时间，单位毫秒

    // 获取聊天历史
    const loadChatHistory = async () => {
      const { orderId, firId, secId } = route.query;
      await fetchChatHistory(orderId, firId, secId, (history) => {
        messages.value = history;
      });
    };

    // 连接 WebSocket
    const startWebSocketConnection = () => {
      connectWebSocket(ws, route, (newMessages) => {
        messages.value = newMessages;
      }, heartbeatInterval, reconnectInterval);
    };

    // 发送消息
    const sendMessageHandler = () => {
      sendMessage(ws, message.value, route);
      message.value = ''; // 发送完消息后清空输入框
    };

    // 提起纠纷
    const disputeHandler = (disputeId) => {
      submitDispute(disputeId, ws);
    };

    // 页面挂载时，连接 WebSocket 并加载历史记录
    onMounted(() => {
      startWebSocketConnection();
      loadChatHistory();
    });

    // 页面卸载时，关闭 WebSocket 连接
    onBeforeUnmount(() => {
      if (ws.value) {
        ws.value.close();
      }
    });

    return {
      message,
      messages,
      sendMessage: sendMessageHandler,
      disputeHandler
    };
  }
};
</script>

<style scoped>
/* 整体聊天框容器 */
.chat-container {
  display: flex;
  flex-direction: column;
  background-color: #f4f4f9;
  border-radius: 10px;
  max-width: 500px;
  margin: 0 auto;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  height: 600px;
}

/* 聊天头部样式 */
.chat-header {
  background-color: #4CAF50;
  color: white;
  padding: 15px;
  text-align: center;
  font-size: 20px;
  font-weight: bold;
}

/* 聊天消息区域 */
.chat-messages {
  flex-grow: 1;
  padding: 15px;
  overflow-y: auto;
  background-color: #ffffff;
  border-bottom: 1px solid #ddd;
}

/* 每条聊天消息 */
.chat-message {
  margin-bottom: 15px;
  font-size: 14px;
  line-height: 1.5;
}

.chat-message strong {
  color: #4CAF50;
}

/* 输入框区域 */
.chat-input {
  display: flex;
  align-items: center;
  padding: 15px;
  background-color: #fff;
  border-top: 1px solid #ddd;
}

.chat-input input {
  flex-grow: 1;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 20px;
  margin-right: 10px;
  font-size: 14px;
  outline: none;
}

.chat-input input:focus {
  border-color: #4CAF50;
}

.chat-input button {
  background-color: #4CAF50;
  color: white;
  padding: 10px 20px;
  border: none;
  border-radius: 20px;
  cursor: pointer;
  font-size: 14px;
}

.chat-input button:hover {
  background-color: #45a049;
}

/* 添加自适应响应 */
@media (max-width: 600px) {
  .chat-container {
    width: 100%;
    height: 100%;
  }

  .chat-header h2 {
    font-size: 18px;
  }

  .chat-input input {
    font-size: 12px;
  }

  .chat-input button {
    font-size: 12px;
  }
}
</style>
